/* Copyright (c) 2004-2022 peigen.info. All rights reserved. */

package com.gitee.peigenlpy.actable.example.repository.entity.enums;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.beetl.sql.annotation.entity.EnumValue;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <b>(CustomerStatus)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/7/28
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum CustomerStatus implements Messageable {
    /** 为了对抗 Alibaba Java Coding 而存在的注释 */
    Init("Init", "初始化"),
    Quick_Register("Quick_Register", "快速注册客户"),
    Un_Activated("Un_Activated", "注册未激活"),
    Un_Cert("Un_Cert", "未实名"),
    Cert_Pass("Cert_Pass", "已实名"),
    Normal("Normal", "正常客户"),
    Freeze("Freeze", "被冻结"),
    Un_Subscribe("Un_Subscribe", "被注销"),
    /** 不晓得啷格回事 */
    UNKNOWN("--", "不晓得啷格回事"),
    ;

    /** 枚举值 */
    @EnumValue
    String code;

    /** 枚举描述 */
    String message;


    /**
     * 通过枚举<code>code</code>获得枚举
     *
     * @param code 枚举值
     *
     * @return CustomerStatus
     */
    public static CustomerStatus getByCode(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.code()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 通过枚举<code>name</code>获得枚举
     *
     * @param name 枚举名
     *
     * @return CustomerStatus
     */
    public static CustomerStatus getByName(String name) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equalsIgnoreCase(name, innerEnum.name()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 获取全部枚举
     *
     * @return 枚举列表
     */
    public static List<CustomerStatus> getAllEnum() {
        return Lists.newArrayList(values());
    }

    /**
     * 获取全部枚举值
     *
     * @return Code列表
     */
    public static List<String> getAllEnumCode() {
        return getAllEnum().stream().map(CustomerStatus::code).collect(Collectors.toList());
    }

    /**
     * 通过code获取msg
     *
     * @param code 枚举值
     *
     * @return String
     */
    public static String getMsgByCode(String code) {
        return Optional.ofNullable(code).map(
                str ->
                        Optional.ofNullable(getByCode(str))
                                .map(CustomerStatus::message).orElse(UNKNOWN.message())
        ).orElse(null);
    }

    /**
     * 获取枚举code
     *
     * @param innerEnum 枚举
     *
     * @return code
     */
    public static String getCode(CustomerStatus innerEnum) {
        return Optional.ofNullable(innerEnum).map(CustomerStatus::code).orElse(UNKNOWN.code());
    }

    /**
     * map
     *
     * @return Map
     */
    public static Map<String, String> maps() {
        return Stream.of(values()).collect(Collectors.toMap(CustomerStatus::code, CustomerStatus::message));
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的字段值
     *
     * @param enumValue    枚举
     * @param mapper       mapper
     * @param defaultValue 默认值
     * @param <T>          对象
     *
     * @return 对象
     */
    public static <T> T safeGet(CustomerStatus enumValue, Function<CustomerStatus, T> mapper, T defaultValue) {
        return Objects.isNull(enumValue) ? defaultValue : mapper.apply(enumValue);
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的字段值，默认返回值是null
     *
     * @param enumValue 枚举
     * @param mapper    mapper
     * @param <T>       对象
     *
     * @return 对象
     */
    public static <T> T safeGet(CustomerStatus enumValue, Function<CustomerStatus, T> mapper) {
        return safeGet(enumValue, mapper, null);
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的code值，默认返回值是null
     *
     * @param enumValue 枚举
     *
     * @return String
     */
    public static String safeGetCode(CustomerStatus enumValue) {
        return safeGet(enumValue, Messageable::code, null);
    }

    /**
     * 判断给定的枚举，是否在列表中
     *
     * @param values 列表
     *
     * @return Boolean
     */
    public boolean isInList(CustomerStatus... values) {
        for (CustomerStatus e : values) {
            if (this == e) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return Returns the code.
     */
    @Override
    public String code() {
        return code;
    }

    /**
     * @return Returns the message.
     */
    @Override
    public String message() {
        return message;
    }
}